﻿#if !ZUNE && !SILVERLIGHT

#region COPYRIGHT

/*
    Copyright (c) 2008, 2009, 2010 
        Roland Rosenkranz (Glatzemann@email.de)
*/

#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Collision.

    starLiGHT.Collision is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Collision is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Collision.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Collision are available on request.
*/

#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using starLiGHT.Physic.TwoD;
using starLiGHT.Render;

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1486                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-10-08 15:55:47 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Physic.TwoD
{
    public class DebugRender : DebugDraw
    {
        PrimitiveBatch primitiveBatch;
        AABB worldAABB;
        float screenWidth;
        float screenHeight;
        public Vector2 screenOffset = Vector2.Zero;

        public Matrix ViewMatrix
        {
            get { return primitiveBatch.ViewMatrix; }
            set { primitiveBatch.ViewMatrix = value; }
        }

        public Matrix ProjectionMatrix
        {
            get { return primitiveBatch.ProjectionMatrix; }
            set { primitiveBatch.ProjectionMatrix = value; }
        }

        public Matrix WorldMatrix
        {
            get { return primitiveBatch.WorldMatrix; }
            set { primitiveBatch.WorldMatrix = value; }
        }

        public override void updateCamera(ICamera2D camera, bool flipY)
        {
            primitiveBatch.ProjectionMatrix = Matrix.CreateOrthographic(camera.Size.X / camera.Zoom, (-camera.Size.Y * (flipY ? -1f : 1f)) / camera.Zoom, 0.0f, 1.0f);
            primitiveBatch.WorldMatrix = Matrix.CreateTranslation(-camera.Size.X * 0.5f, -camera.Size.Y * 0.5f, 0.0f);
            primitiveBatch.ViewMatrix = camera.CameraMatrixNoScale;
        }

        public DebugRender(GraphicsDevice graphicsDevice, ICamera camera, AABB worldAABB, float screenWidth, float screenHeight, float scale)
        {
            primitiveBatch = new PrimitiveBatch(graphicsDevice);

            this.worldAABB = worldAABB;
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;

            float worldWidth = worldAABB.upperBound.X - worldAABB.lowerBound.X;
            float worldHeight = worldAABB.upperBound.Y - worldAABB.lowerBound.Y;
        }

        public override void DrawPolygon(Vector2[] vertices, int vertexCount, Color color)
        {
            primitiveBatch.Begin(PrimitiveType.LineList);

            for (int i = 1; i < vertexCount; i++)
            {
                primitiveBatch.AddVertex(screenOffset + vertices[i - 1], color);
                primitiveBatch.AddVertex(screenOffset + vertices[i], color);
            }
            primitiveBatch.AddVertex(screenOffset + vertices[vertexCount - 1], color);
            primitiveBatch.AddVertex(screenOffset + vertices[0], color);

            primitiveBatch.End();
        }

        public override void DrawSolidPolygon(Vector2[] vertices, int vertexCount, Color color)
        {
            if (vertexCount > 2)
            {
#if XNA4
                //primitiveBatch.Begin(PrimitiveType.LineList);
                //primitiveBatch.AddVertex(Vector2.Zero, Color.White);
                //primitiveBatch.AddVertex(new Vector2(200.0f, 200.0f), Color.White);
                //primitiveBatch.End();

                primitiveBatch.Begin(PrimitiveType.TriangleList);
                Color fillColor = Color.Red; // new Color((byte)(color.R / 2), (byte)(color.G / 2), (byte)(color.B / 2), (byte)128);

                for (int i = 2; i < vertexCount; i++)
                {
                    primitiveBatch.AddVertex(screenOffset + vertices[0], fillColor);
                    primitiveBatch.AddVertex(screenOffset + vertices[i - 1], fillColor);
                    primitiveBatch.AddVertex(screenOffset + vertices[i], fillColor);
                }

                primitiveBatch.End();

#else
                primitiveBatch.Begin(PrimitiveType.TriangleFan);
                Color fillColor = new Color((byte)(color.R / 2), (byte)(color.G / 2), (byte)(color.B / 2), (byte)128);

                for (int i = 0; i < vertexCount; i++)
                {
                    primitiveBatch.AddVertex(screenOffset + vertices[i], fillColor);
                }

                primitiveBatch.End();

#endif
            }

            primitiveBatch.Begin(PrimitiveType.LineList);

            for (int i = 1; i < vertexCount; i++)
            {
                primitiveBatch.AddVertex(screenOffset + vertices[i - 1], color);
                primitiveBatch.AddVertex(screenOffset + vertices[i], color);
            }
            primitiveBatch.AddVertex(screenOffset + vertices[vertexCount - 1], color);
            primitiveBatch.AddVertex(screenOffset + vertices[0], color);

            primitiveBatch.End();
        }

        public override void DrawCircle(Vector2 center, float radius, Color color)
        {
            float k_segments = 16.0f;
            float k_increment = 2.0f * PhysicSettings.b2_pi / k_segments;
            float theta = 0.0f;
            Vector2 v = Vector2.Zero;
            Vector2 first = Vector2.Zero;
            Vector2 last = Vector2.Zero;

            primitiveBatch.Begin(PrimitiveType.LineList);

            for (int i = 0; i < k_segments; ++i)
            {
                if (i == 0)
                {
                    first = last = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                }
                else
                {
                    if (i > 1) last = v;
                    v = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                    primitiveBatch.AddVertex(screenOffset + last, color);
                    primitiveBatch.AddVertex(screenOffset + v, color);
                }

                theta += k_increment;
            }

            primitiveBatch.AddVertex(screenOffset + v, color);
            primitiveBatch.AddVertex(screenOffset + first, color);

            primitiveBatch.End();
        }

        public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, Color color)
        {
            float k_segments = 16.0f;
            float k_increment = 2.0f * PhysicSettings.b2_pi / k_segments;
            float theta = 0.0f; 
            
            Color fillColor = new Color((byte)(color.R / 2), (byte)(color.G / 2), (byte)(color.B / 2), (byte)128);

            Vector2 v = Vector2.Zero;

#if XNA4
            primitiveBatch.Begin(PrimitiveType.TriangleList);
#else
            primitiveBatch.Begin(PrimitiveType.TriangleFan);
#endif

            for (int i = 0; i < k_segments; ++i)
            {
                v = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                primitiveBatch.AddVertex(screenOffset + v, fillColor);
                theta += k_increment;
            }

            primitiveBatch.End();

            primitiveBatch.Begin(PrimitiveType.LineList);
            theta = 0.0f;
            Vector2 last = Vector2.Zero;
            Vector2 first = Vector2.Zero;

            for (int i = 0; i < k_segments; ++i)
            {
                if (i == 0)
                {
                    first = last = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                }
                else
                {
                    if (i > 1) last = v;
                    v = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                    primitiveBatch.AddVertex(screenOffset + last, color);
                    primitiveBatch.AddVertex(screenOffset + v, color);
                }

                theta += k_increment;
            }

            primitiveBatch.AddVertex(screenOffset + v, color);
            primitiveBatch.AddVertex(screenOffset + first, color);

            primitiveBatch.End();

            Vector2 p = center + radius * axis;
            primitiveBatch.Begin(PrimitiveType.LineList);
            primitiveBatch.AddVertex(screenOffset + center, color);
            primitiveBatch.AddVertex(screenOffset + p, color);
            primitiveBatch.End();
        }

        public override void DrawSegment(Vector2 p1, Vector2 p2, Color color)
        {
            primitiveBatch.Begin(PrimitiveType.LineList);

            primitiveBatch.AddVertex(screenOffset + p2, color);
            primitiveBatch.AddVertex(screenOffset + p1, color);

            primitiveBatch.End();
        }

        public override void DrawTransform(Transform xf)
        {
            throw new NotImplementedException();
        }

        public override void DrawPoint(Vector2 p, float size, Color color)
        {
#if XNA4
            throw new NotImplementedException();
#else
            primitiveBatch.Begin(PrimitiveType.PointList);
            primitiveBatch.PointSize = size;

            primitiveBatch.AddVertex(screenOffset + p, color);

            primitiveBatch.End();
#endif
        }

        public override void DrawString(SpriteBatch spriteBatch, SpriteFont font, int x, int y, string text)
        {
            if (!String.IsNullOrEmpty(text))
                spriteBatch.DrawString(font, text, new Vector2(x, y), Color.White);
        }

        public override void DrawAABB(ref AABB aabb, Color color)
        {
            primitiveBatch.Begin(PrimitiveType.LineList);

            primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.lowerBound.Y), color);
            primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.lowerBound.Y), color);

            primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.lowerBound.Y), color);
            primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.upperBound.Y), color);

            primitiveBatch.AddVertex(new Vector2(aabb.upperBound.X, aabb.upperBound.Y), color);
            primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.upperBound.Y), color);

            primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.upperBound.Y), color);
            primitiveBatch.AddVertex(new Vector2(aabb.lowerBound.X, aabb.lowerBound.Y), color);

            primitiveBatch.End();
        }
    }
}

#endif